﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;

namespace WpfApp2
{
    using System.Collections.ObjectModel;
    using System.ComponentModel;
    using System.Windows.Controls;

    public class MainViewModel : INotifyPropertyChanged
    {
        // 可用控件类型集合
        public ObservableCollection<string> ControlTypes { get; } = new ObservableCollection<string>
    {
        "文本参数",
        "数字参数"
    };

        // 当前选择的控件类型
        private string _selectedType;
        public string SelectedType
        {
            get => _selectedType;
            set
            {
                _selectedType = value;
                OnPropertyChanged();
                UpdateCurrentParameter();
            }
        }

        // 当前参数对象
        private ParameterBase _currentParameter;
        public ParameterBase CurrentParameter
        {
            get => _currentParameter;
            set { _currentParameter = value; OnPropertyChanged(); }
        }

        // 已添加的参数集合
        public ObservableCollection<ParameterBase> AddedParameters { get; } = new ObservableCollection<ParameterBase>();

        // Add命令
        public ICommand AddCommand => new RelayCommand(AddParameter);

        private void UpdateCurrentParameter()
        {
            CurrentParameter = SelectedType switch
            {
                "文本参数" => new TextParameter { TypeName = "文本参数" },
                "数字参数" => new NumberParameter { TypeName = "数字参数" },
                _ => null
            };
        }

        private void AddParameter()
        {
            if (CurrentParameter != null)
            {
                var clone = CloneParameter(CurrentParameter);
                AddedParameters.Add(clone);
            }
        }

        private ParameterBase CloneParameter(ParameterBase param)
        {
            return param switch
            {
                TextParameter p => new TextParameter { Name = p.Name },
                NumberParameter p => new NumberParameter { NumberValue = p.NumberValue },
                _ => null
            };
        }

        public event PropertyChangedEventHandler PropertyChanged;
        protected void OnPropertyChanged([CallerMemberName] string name = null) =>
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
    }
    public class RelayCommand : ICommand
    {
        private readonly Action _execute;
        private readonly Func<bool> _canExecute;

        public RelayCommand(Action execute, Func<bool> canExecute = null)
        {
            _execute = execute ?? throw new ArgumentNullException(nameof(execute));
            _canExecute = canExecute;
        }

        public bool CanExecute(object parameter) => _canExecute?.Invoke() ?? true;

        public void Execute(object parameter) => _execute();

        public event EventHandler CanExecuteChanged
        {
            add => CommandManager.RequerySuggested += value;
            remove => CommandManager.RequerySuggested -= value;
        }

        // 可选：手动触发CanExecute状态更新
        public void RaiseCanExecuteChanged() => CommandManager.InvalidateRequerySuggested();
    }
    public class Preson
    {
        public string Name;
        public int age;
    }

}
